package com.jblog.core.service.blog.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jblog.core.CoreUtil;
import com.jblog.core.dao.blog.CategoryDao;
import com.jblog.core.model.blog.Category;
import com.jblog.core.model.blog.param.CategoryParam;
import com.jblog.core.service.blog.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {
    
    @Autowired private CategoryDao categoryDao;

    @Override
    public Category findById(Long id) {
        return findOne(new CategoryParam(id));
    }

    @Override
    public Category findByAlisa(String s) {
        return findOne(new CategoryParam(s));
    }

    @Override
    public List<Category> findAll(CategoryParam p) {
        return categoryDao.getAll(p);
    }

    @Override
    public List<Category> findByParam(CategoryParam p) {
        return categoryDao.byParam(p);
    }
    
    private Category findOne(CategoryParam p) {
        List<Category> es = findByParam(p);
        if (null != es && es.size() == 1) {
            return es.get(0);
        } 
        return null;
    }

    @Override
    public void enDisable(Category e, boolean able) {
        if (!CoreUtil.checkStatus(e.getStatus(), able)) {
            update(new Category(e.getId(), CoreUtil.toStatus(able)));
        }
    }

    @Override
    public void save(Category e) {
        categoryDao.save(e);
    }

    @Override
    public void update(Category e) {
        categoryDao.update(e);
    }

    @Override
    public void delete(Long id) {
        categoryDao.deleteByPk(id);
    }
 
}